﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using System.Linq;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using ezCloud.Api.Models;
using ezCloud.Api.Providers;
using ezCloud.Api.Results;
using ezCloud.Data;
using System.Configuration;
using System.Net;
using ezCloud.Api.Services;
using Microsoft.Owin.Testing;
using System.Web.Script.Serialization;
using System.IO;
using Microsoft.Owin.Security.Infrastructure;
using ezCloud.Api.Common;
using System.Data;

namespace ezCloud.Api.Controllers
{
   
    [RoutePrefix("api/HotelApp")]
    public class HotelAppController : ezController
    {
        private Common.MemoryCacher memCache = new Common.MemoryCacher();
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("OccupancyForecast")]
        public async Task<IHttpActionResult> GetOccupancyForecast(DateTime from)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var context = new ezCloudDataContext();
            var user = Common.UserUtitily.getUserProfile();

            var currentHotel = context.Hotels.Where(ht => ht.HotelId == user.HotelId).FirstOrDefault();

            var firstDayOfMonth = new DateTime(from.Year, from.Month, 1).Date;
            var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1).Date;

            //var cacheKey = "DASHBOARD_OCC" + user.HotelId + "_" + string.Format("{0:yyyyMMdd}", from) + "_" + 0;
            //var timelineCache = memCache.GetValue(cacheKey);
            //if (timelineCache != null)
            //    return Ok(timelineCache);

            List<OccupancyForecastMonthChart> listOccupancyForecastMonth = new List<OccupancyForecastMonthChart>();
         
            //Note: khong dieu chinh thoi gian ve moc 0 gio cua khach san vi SP nay da thuc hien roi
            var RoomSalesForecast = Utilities.GetRoomSalesForecast(firstDayOfMonth, lastDayOfMonth);
           
            foreach (DataRow row in RoomSalesForecast.Rows)
            {

                var date =(DateTime) row["Dates"];
                var total =  row["Occ"]; 
                var oitem = new OccupancyForecastMonthChart();
                oitem.Date = date;
                oitem.Day = date.Day;
                oitem.Total =Convert.ToDecimal( total);
                listOccupancyForecastMonth.Add(oitem);

            }
            var retval = new
            {
                listOccupancyForecastMonth = listOccupancyForecastMonth
            };

           // memCache.Add(cacheKey, retval, DateTimeOffset.UtcNow.AddHours(1));
            
            return Ok(retval);
        }
    }


   
    public class objectHotelAppConfig
    {

        public HotelAppCode HotelApp { get; set; }
        public int IsSelected { get; set; }
    }
    public class RevenueMonthChart
    {
        public DateTime Date { get; set; }
        public int Day { get; set; }
        public float Amount { get; set; }
    }

    public class OccupancyForecastMonthChart
    {
        public DateTime Date { get; set; }
        public int Day { get; set; }
        public decimal Total { get; set; }
    }
}
